package com.hzf.ucenter.admin.controller.manage;

import com.hzf.common.base.BaseController;
import com.hzf.ucenter.dao.model.UcenterUserOauth;
import com.hzf.ucenter.dao.model.UcenterUserOauthExample;
import com.hzf.ucenter.rpc.api.UcenterUserOauthService;
import com.hzf.upms.common.constant.UpmsResult;
import com.hzf.upms.common.constant.UpmsResultConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 第三方用户管理
 * Created by  2017/01/12.
 */

@Controller
@Api(value = "平台基础用户管理", description = "基础用户管理")
@RequestMapping("/manage/ucenterUserThrid")
public class UcenterUserThirdController extends BaseController {
    private static Logger _log = LoggerFactory.getLogger(UcenterUserThirdController.class);

    @Autowired
    private UcenterUserOauthService ucenterUserOauthService;

    @ApiOperation(value = "第三方用户关联首页")
    @RequiresPermissions("ucenter:userthird:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/userThird/index.jsp";
    }

    @ApiOperation(value = "第三方关联用户列表")
    @RequiresPermissions("ucenter:userthird:read")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                       @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
                       @RequestParam(required = false, value = "sort") String sort,
                       @RequestParam(required = false, value = "order") String order) {

        UcenterUserOauthExample ucenterUserOauthExample = new UcenterUserOauthExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            ucenterUserOauthExample.setOrderByClause(sort + " " + order);
        }
        List<UcenterUserOauth> rows = ucenterUserOauthService.selectByExampleForOffsetPage(ucenterUserOauthExample, offset, limit);

        long total = ucenterUserOauthService.countByExample(ucenterUserOauthExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增用户")
    @RequiresPermissions("ucenter:userthird:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(ModelMap modelMap) {


        return "/manage/userThird/create.jsp";
    }

    @ApiOperation(value = "新增用户")
    @RequiresPermissions("ucenter:userthird:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(UcenterUserOauth ucenterUserOauth) {
        long time = System.currentTimeMillis();
        int count = ucenterUserOauthService.insertSelective(ucenterUserOauth);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除用户")
    @RequiresPermissions("ucenter:userthird:delete")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = ucenterUserOauthService.deleteByPrimaryKeys(ids);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改用户")
    @RequiresPermissions("ucenter:userthird:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        System.out.println("id="+id);
        UcenterUserOauth ucenterUserOauth = ucenterUserOauthService.selectByPrimaryKey(id);
        modelMap.put("ucenterUserOauth", ucenterUserOauth);
        return "/manage/userThird/update.jsp";
    }

    @ApiOperation(value = "修改用户")
    @RequiresPermissions("ucenter:userthird:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") int id, UcenterUserOauth ucenterUserOauth) {
        ucenterUserOauth.setId(id);
        int count = ucenterUserOauthService.updateByPrimaryKeySelective(ucenterUserOauth);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }


}

